package com.gin.no_gay_alliance.activity;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.databinding.ActivityUserCenterBinding;
import com.gin.no_gay_alliance.databinding.MergeToolbarViewpager2Binding;
import com.gin.no_gay_alliance.fragment.user.UserCenterFragment;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用户中心活动
 */
public class UserCenterActivity extends BaseBindingActivity<ActivityUserCenterBinding> {
    public static final Pattern PATTERN_UID = Pattern.compile("uid=(\\d+)");
    public static final Pattern PATTERN_USERNAME = Pattern.compile("username=([^&]+)");
    long userId;
    String username;
    Long userIdFromUrl;
    String usernameFromUrl;
    MergeToolbarViewpager2Binding mb;


    /**
     * 解析意图
     *
     * @param intent 意图
     */
    @Override
    protected void handleIntent(Intent intent) {
        // 从意图中读取用户名或用户id
        userId = intent.getLongExtra(UserCenterFragment.ARG_USER_ID, -1L);
        username = intent.getStringExtra(UserCenterFragment.ARG_USERNAME);
        // 从url获取
        String url = intent.getDataString();
        if (!ObjectUtils.isEmpty(url)) {
            Matcher uidMatcher = PATTERN_UID.matcher(url);
            if (uidMatcher.find()) {
                userIdFromUrl = Long.valueOf(uidMatcher.group(1));
            }
            Matcher usernameMatcher = PATTERN_USERNAME.matcher(url);
            if (usernameMatcher.find()) {
                //解码
                try {
                    usernameFromUrl = URLDecoder.decode(usernameMatcher.group(1), url.contains("__inchst=UTF8") ? "utf-8" : "GB18030");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onCreate() {
        mb = MergeToolbarViewpager2Binding.bind(b.getRoot());

        // 初始化vp2
        mb.vp2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return UserCenterFragment.getInstance(userId, userIdFromUrl, username, usernameFromUrl);
            }

            @Override
            public int getItemCount() {
                return 1;
            }
        });
        mb.vp2.setUserInputEnabled(false);

    }

    /**
     * 装载右上角菜单
     *
     * @param menu         菜单
     * @param menuInflater 菜单渲染器
     */
    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {

    }

    @Override
    protected Toolbar initToolbar() {
        return MergeToolbarViewpager2Binding.bind(b.getRoot()).toolbar;
    }

    public static void startActivity(Context context, long userId) {
        Intent intent = new Intent(context, UserCenterActivity.class);
        intent.putExtra(UserCenterFragment.ARG_USER_ID, userId);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String username) {
        Intent intent = new Intent(context, UserCenterActivity.class);
        intent.putExtra(UserCenterFragment.ARG_USERNAME, username);
        context.startActivity(intent);
    }

}